home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
Source.bin
/
TreeNode.java
< prev
next >
Wrap
Text File
|
1998-08-21
|
9KB
|
379 lines
package symantec.itools.awt;
import java.awt.Image;
// 02/10/97 RKM Added data object
// 02/27/97 RKM Merged in accessors for getParent, getChild, & getSibling
// 07/25/97 CAR marked fields transient as needed will have to re-test after event handling code is changed
// implements interface java.io.Serializable
// 08/20/97 LAB Added dependency on having a reference to the containing TreeView instance.
// This modified the constructors, and deprecated the existing constructors.
// TreeNode now calls TreeView's triggerRedraw when it needs to refresh its
// visible state. Addresses Mac Bug #4372
// 08/21/97 LAB Actually un-commented the fix for calling triggerRedraw (oy!).
// 02/09/98 DS Added support for hiding/showing nodes (TreeNode.setHidden(boolean)
/**
* This is a single node in the TreeView panel.
* It displays text and optionally one of two images depending on its state,
* collapsed or expanded.
* It also may have an object associated with it that doesn't get displayed.
* @see TreeView
*/
public class TreeNode implements java.io.Serializable
{
TreeNode sibling;
TreeNode child;
TreeNode parent;
String text;
transient Image collapsedImage;
transient Image expandedImage;
int numberOfChildren;
Object dataObject;
TreeView treeView;
int depth = -1;
boolean isExpanded = false;
boolean hidden;
//constructors
/**
* Constructs a default TreeNode.
*/
public TreeNode() { }
/**
* @deprecated
* @see TreeNode#TreeNode(java.lang.String, symantec.itools.awt.TreeView)
*/
public TreeNode(String text)
{
this(text, null, null, null);
}
/**
* @deprecated
* @see TreeNode#TreeNode(java.lang.String, java.awt.Image, java.awt.Image, symantec.itools.awt.TreeView)
*/
public TreeNode(String text, Image collapsedImage, Image expandedImage)
{
this(text, collapsedImage, expandedImage, null);
}
/**
* Constructs a TreeNode with the given text label.
* @param text the text to display for this node
* @param treeView the instance of TreeView whose node this is.
* Typically "this"
*/
public TreeNode(String text, TreeView treeView)
{
this(text, null, null, treeView);
}
/**
* Constructs a TreeNode with the given text label, and collapsed and
* expanded images.
* @param text the text to display for this node
* @param collapsedImage the image to use when this node is collapsed, hiding
* all of its child nodes
* @param expandedImage the image to use when this node is expanded, showing
* all of its child nodes
* @param treeView the instance of TreeView whose node this is.
* Typically "this"
*/
public TreeNode(String text, Image collapsedImage, Image expandedImage, TreeView treeView)
{
this.text = text;
this.sibling = null;
this.child = null;
this.collapsedImage = collapsedImage;
this.expandedImage = expandedImage;
this.numberOfChildren = 0;
this.dataObject = null;
this.treeView = treeView;
}
/**
* Notes the current depth of this node.
* @param depth
* @see #getDepth
*/
void setDepth(int depth)
{
this.depth = depth;
}
/**
* Gets the depth of this node as previously noted.
* @return the depth of this node
*/
public int getDepth()
{
return depth;
}
/**
* Determines whether this node is expanded.
* A node is expanded if its child nodes are visible.
* @return true if the node is expanded, false if it is collapsed
*/
public boolean isExpanded()
{
return isExpanded;
}
/**
* Determines whether this node is expandable.
* A node is expandable if it has one or more child nodes.
* @return true if the node is expandable, false if not
*/
public boolean isExpandable()
{
return (child!=null);
}
/**
* Sets a flag indicating that this node is expanded, if it is expandable.
*/
public void expand()
{
if (isExpandable())
{
isExpanded=true;
if (treeView != null)
treeView.triggerRedraw();
}
}
/**
* Sets a flag indicating that this node is not expanded.
*/
public void collapse()
{
isExpanded = false;
if (treeView != null)
treeView.triggerRedraw();
}
/**
* Toggles the node state between collapsed and expanded, if the node
* is expandable.
*/
public void toggle()
{
if (isExpanded)
{
collapse();
}
else if (isExpandable())
{
expand();
}
}
/**
* Gets the proper image for this node in its current state, expanded or collapsed.
* @return the current image for this node in its current state
*/
public Image getImage()
{
return ((isExpanded && (expandedImage != null))
? expandedImage
: collapsedImage);
}
/**
* Sets the image to use for this node when it is expanded.
* @param image the image to use when this node is expanded
* @see #setCollapsedImage
* @see #getImage
*/
public void setExpandedImage(Image image)
{
expandedImage = image;
if (isExpanded() && treeView != null)
treeView.triggerRedraw();
}
/**
* Sets the image to use for this node when it is not expanded.
* @param image the image to use when this node is collapsed
* @see #setExpandedImage
* @see #getImage
*/
public void setCollapsedImage(Image image)
{
collapsedImage = image;
if (!isExpanded() && treeView != null)
treeView.triggerRedraw();
}
/**
* Gets the current text label for this node.
* @return the current text label for this node
* @see #setText
*/
public String getText()
{
return text;
}
/**
* Sets a new text label for this node.
* @param s the new text label for this node
* @see #getText
*/
public void setText(String s)
{
text = new String(s);
if (treeView != null)
treeView.triggerRedraw();
}
/**
* Gets the object associated with this node.
* This object does not get displayed.
* @return the object associated with this node
* @see #setDataObject
*/
public Object getDataObject()
{
return dataObject;
}
/**
* Sets an object to associate with this node.
* This object does not get displayed.
* @param theObject an object to associate with this node
* @see #getDataObject
*/
public void setDataObject(Object theObject)
{
dataObject = theObject;
}
/**
* Gets the parent of this node.
* @return this node's parent node
* @see #getChild
* @see #getSibling
*/
public TreeNode getParent()
{
return parent;
}
/**
* Gets the child of this node.
* @return this node's child node
* @see #getParent
* @see #getSibling
*/
public TreeNode getChild()
{
return child;
}
/**
* Gets the next sibling of this node.
* @return this node's next sibling node
* @see #getChild
* @see #getParent
*/
public TreeNode getSibling()
{
return sibling;
}
/**
* Sets whether this node is hidden.
* @param f <code>true</code> to make this node is hidden
* @see #isHidden
*/
public void setHidden(boolean f)
{
hidden = f;
if(treeView != null)
{
treeView.repaint(true);
}
}
/**
* Gets whether this node is hidden.
* @return <code>true</code> if this node is hidden
* @see #setHidden
*/
public boolean isHidden()
{
return (hidden);
}
/**
* Determines whether any sibling nodes are visible.
* @return <code>true</code> if a sibling node is visible
* @see #isAChildVisible
*/
public boolean isASiblingVisible()
{
TreeNode node;
if(sibling == null)
{
return (false);
}
if(!(sibling.isHidden()))
{
return (true);
}
node = sibling;
while(node.sibling != null)
{
node = node.sibling;
if(!(node.isHidden()))
{
return (true);
}
}
return (false);
}
/**
* Determines whether any child nodes are visible.
* @return <code>true</code> if a child node is visible
* @see #isASiblingVisible
*/
public boolean isAChildVisible()
{
TreeNode node;
if(child == null)
{
return (false);
}
if(!(child.isHidden()))
{
return (true);
}
if(child.sibling == null)
{
return (false);
}
return (child.isASiblingVisible());
}
}